package com.hebeu.device.controller;

import com.github.pagehelper.PageInfo;
import com.hebeu.device.pojo.DMaintenance;
import com.hebeu.device.pojo.DUser;
import com.hebeu.device.service.DMaintenanceService;
import com.hebeu.device.service.DPayoffService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;


/**
 * @author leiyufei
 * @date 2019-6-16
 * 维修记录相关控制器
 */
@RestController
@RequestMapping("/dMaintenance/")
public class DMaintenanceController {

    @Resource
    private DMaintenanceService dMaintenanceService;
    @Resource
    private DPayoffService dPayoffService;

    /**
     * 通过维修人员查询维修记录
     * @param dMaintenance 维修记录
     * @return 维修记录列表和登录用户信息
     */
    @RequestMapping("findPersonalDMaintenanceList")
    public HashMap<String,Object> findPersonalDMaintenanceList(DMaintenance dMaintenance,HttpSession httpSession){
        DUser dUser = (DUser) httpSession.getAttribute("user");
        System.out.println("登录用户信息："+dUser.getuId());
        dMaintenance.setMainPerson(dUser.getuId());
        List<DMaintenance> dMaintenanceList = dMaintenanceService.selectDMaintenanceListByMainPerson(dMaintenance);
        PageInfo<DMaintenance> dMaintenancePageInfo = new PageInfo<>(dMaintenanceList);
        //获取当前登录用户（待修改）
        HashMap<String,Object> map = new HashMap<>();
        map.put("list",dMaintenancePageInfo);
        map.put("dUser",dUser);

        return map;
    }

    /**
     * 查询维修记录详情
     * @param mainId 维修记录id
     * @return 维修记录详情和登录用户信息
     */
    @RequestMapping("findDMaintenanceInfo")
    public HashMap<String,Object> findDMaintenanceInfo(Integer mainId,HttpSession httpSession){

        DMaintenance dMaintenance = dMaintenanceService.selectDMaintenanceByMainId(mainId);
        //获取当前登录用户（待修改）
        DUser dUser = (DUser) httpSession.getAttribute("user");
        HashMap<String,Object> map = new HashMap<>();
        map.put("info",dMaintenance);
        map.put("dUser",dUser);

        return map;
    }

    /**
     * 修改维修记录
     * @param dMaintenance 维修记录
     *
     */
    @PostMapping("updateDMaintenance")
    public String updateDMaintenance(DMaintenance dMaintenance){

        if (dMaintenance.getMainId()==null){
            return "维修记录id不能为空";
        }
        try {
            try {
                if (dMaintenance.getMainStatus()==2){
                    //生成结算信息
                    dPayoffService.addDPayoff(dMaintenance.getServiceId());
                }
            }catch (Exception e){
                e.printStackTrace();
                return "生成结算信息失败！";
            }
            //修改维修记录状态
            dMaintenanceService.updateDMaintenance(dMaintenance);
        }catch (Exception e){
            e.printStackTrace();
            return "修改维修记录失败！";
        }

        return "修改成功！";
    }


}
